package com.zz.common.utils;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Base64;

public abstract class AlgorithmUtil {
	public static String md5(String key) {
		try {
			char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
			MessageDigest md;
			md = MessageDigest.getInstance("MD5");
			byte[] buf = key.getBytes();
			md.update(buf, 0, buf.length);
			byte[] bytes = md.digest();
			StringBuilder sb = new StringBuilder(32);
			for (byte b : bytes) {
				sb.append(hex[((b >> 4) & 0xF)]).append(hex[((b >> 0) & 0xF)]);
			}
			key = sb.toString();
		} catch (NoSuchAlgorithmException e) {
			e = null;
		}
		return key;
	}
	
	public static String base64Encode(String str) {
		if (null == str) {
			return null;
		}
		return Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);
	}
	
	public static String encodeUrl(String str) {
		String result = "";
		try {
			result = URLEncoder.encode(str,"UTF-8");
			result = URLEncoder.encode(result);
		} catch (Exception e) {
			e = null;
		}
		return result;
	}
}
